/**
 * 
 */
package reco.db.validation;

import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.commons.lang.StringUtils.isBlank;
import reco.db.Constraint;

public final class MaxSizeConstraint extends Constraint<String> {
    private final int size;

    public MaxSizeConstraint(int constraintMaxSize) {
        super("maxSize", "tooLarge");
        checkArgument(constraintMaxSize > 0, "constraintMaxSize must be greater than 0");
        this.size = constraintMaxSize;
    }

    @Override
    public boolean validate(String value) {
        if (isBlank(value)) {
            return true;
        }
        return value.length() <= this.size;
    }

    @Override
    public Object[] getErrorArguments() {
        return new Object[] { this.size };
    }

}
